﻿using System;
using System.Linq;
using ASPNETVN.PORTAL.Components.UI;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class CooperativeStatistics : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                txtYear.Text = DateTime.Today.Year.ToString();

                ddlArea.DataSource = Area.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlArea.DataBind();

                ddlCareer.DataSource = Career.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlCareer.DataBind();

                ddlType.DataSource = Components.Type.SelectCollectionAll(this.ModuleID).OrderBy(x => x.Title);
                ddlType.DataBind();

                this.Statistics();
                this.Search();
            }
        }

        private void Search()
        {
            string where = string.Format("CO.AreaID LIKE '{0}' AND CO.CareerID LIKE '{1}' AND CO.TypeID LIKE '{2}'", ddlArea.SelectedValue, ddlCareer.SelectedValue, ddlType.SelectedValue);
            gvCooperative.DataSource = Cooperative.Search(this.ModuleID, where);
            gvCooperative.DataBind();
        }

        private void Statistics()
        {
            int CountOfCooperative = 0;
            int SumOfLabourQuantity = 0;
            int SumOfWomanLabourQuantity = 0;
            int SumOfCooperatorQuantity = 0;
            int SumOfWomanCooperatorQuantity = 0;
            decimal SumOfCharterCapital = 0;
            decimal SumOfWorkingCapital = 0;
            decimal SumOfFixedCapital = 0;
            decimal SumOfProfit = 0;
            decimal SumOfTax = 0;
            decimal SumOfTurnover = 0;
            int SumOfCooperativePartyMember = 0;
            int CountOfCooperativeHasPartyOrganization = 0;
            int CountOfCooperativeHasTradeUnion = 0;
            int CountOfCooperativeHasTheCommunistYouthLeague = 0;
            int CountOfCooperativeHasWomanUnion = 0;
            int CountOfCooperativeHasInlandMarket = 0;
            int CountOfCooperativeHasOutlandMarket = 0;

            Cooperative.Summary(this.ModuleID, Convert.ToInt32(txtYear.Text), ddlArea.SelectedValue, ddlType.SelectedValue, ddlCareer.SelectedValue,
                                ref CountOfCooperative,
                                ref SumOfLabourQuantity,
                                ref SumOfWomanLabourQuantity,
                                ref SumOfCooperatorQuantity,
                                ref SumOfWomanCooperatorQuantity,
                                ref SumOfCharterCapital,
                                ref SumOfWorkingCapital,
                                ref SumOfFixedCapital,
                                ref SumOfProfit,
                                ref SumOfTax,
                                ref SumOfTurnover,
                                ref SumOfCooperativePartyMember,
                                ref CountOfCooperativeHasPartyOrganization,
                                ref CountOfCooperativeHasTradeUnion,
                                ref CountOfCooperativeHasTheCommunistYouthLeague,
                                ref CountOfCooperativeHasWomanUnion,
                                ref CountOfCooperativeHasInlandMarket,
                                ref CountOfCooperativeHasOutlandMarket);

            lblCountOfCooperative.Text = CountOfCooperative.ToString("N0");
            lblSumOfLabourQuantity.Text = SumOfLabourQuantity.ToString("N0");
            lblSumOfWomanLabourQuantity.Text = SumOfWomanLabourQuantity.ToString("N0");
            lblSumOfCooperatorQuantity.Text = SumOfCooperatorQuantity.ToString("N0");
            lblSumOfWomanCooperatorQuantity.Text = SumOfWomanCooperatorQuantity.ToString("N0");
            lblSumOfCharterCapital.Text = SumOfCharterCapital.ToString("N0");
            lblSumOfWorkingCapital.Text = SumOfWorkingCapital.ToString("N0");
            lblSumOfFixedCapital.Text = SumOfFixedCapital.ToString("N0");
            lblSumOfProfit.Text = SumOfProfit.ToString("N0");
            lblSumOfTax.Text = SumOfTax.ToString("N0");
            lblSumOfTurnover.Text = SumOfTurnover.ToString("N0");
            lblSumOfCooperativePartyMember.Text = SumOfCooperativePartyMember.ToString("N0");
            lblCountOfCooperativeHasPartyOrganization.Text = CountOfCooperativeHasPartyOrganization.ToString("N0");
            lblCountOfCooperativeHasTradeUnion.Text = CountOfCooperativeHasTradeUnion.ToString("N0");
            lblCountOfCooperativeHasTheCommunistYouthLeague.Text = CountOfCooperativeHasTheCommunistYouthLeague.ToString("N0");
            lblCountOfCooperativeHasWomanUnion.Text = CountOfCooperativeHasWomanUnion.ToString("N0");
            lblCountOfCooperativeHasInlandMarket.Text = CountOfCooperativeHasInlandMarket.ToString("N0");
            lblCountOfCooperativeHasOutlandMarket.Text = CountOfCooperativeHasOutlandMarket.ToString("N0");
        }

        protected void btnStastistics_Click(object sender, EventArgs e)
        {
            this.Statistics();
            this.Search();
        }
    }
}